﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper;
using RabbitDownload.Helper.Currency;

using RabbitDownload.WebSite.Pixiv;
using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.WebCurrency
{
    internal class SaveHelper
    {
        public static string TempPath = "";

        /// <summary>
        /// 内部封装的保存list方法,仅用作保存
        /// </summary>
        /// <param name="FileList"></param>
        /// <param name="FileName">例如：文件名 将会变成 文件名.lst</param>
        /// <param name="FloderName">例如：一月图片</param>
        public static string SaveList(List<string> FileList, String FileName, string FloderName)
        {
            string SaveFloder = $"{StaticValueOld.SaveListPath + FloderName}\\";
            FloderHelper.FloderExits(SaveFloder, true);
            String SaveFile = SaveFloder + TextHelper.ReplaceBadChar(FileName) + ".lst";
            if (FileList.Count != 0)
                FileHelper.WriteList(SaveFile, FileList);

            return SaveFile;
        }

        /// <summary>
        /// 自动选择保存用的方法
        /// </summary>
        /// <param name="ImgList"></param>
        /// <param name="FileName">例如：文件名 将会变成 文件名.lst</param>
        /// <param name="FloderName">例如：一月图片</param>
        public static string AutoSave(List<string> ImgList, string FileName, string FloderName)
        {
            if (ImgList.Count == 0)
            {
                Console.WriteLine("没有需要保存的列表，放弃。");
                return "";
            }

            ImgList = ListHelper.ListRepeat(ImgList);
            FileName = TextHelper.ReplaceBadChar(FileName);
            if (FileName == "")
                FileName = "未命名资源";
            Console.WriteLine(Helper.StaticValueOld.SaveImgListFlag);
            if (Helper.StaticValueOld.SaveImgListFlag)
            {
                return SaveListAndAlert(ImgList, FileName, FloderName);
            }
            else
            {
                return DownLoadFileList(ImgList, FloderName);
            }
        }

        /// <summary>
        /// 保存list文件，并且打开提示窗口
        /// </summary>
        /// <param name="ImgList"></param>
        /// <param name="FileName">例如：文件名 将会变成 文件名.lst</param>
        /// <param name="FloderName">例如：一月图片</param>
        public static string SaveListAndAlert(List<string> ImgList, String FileName, string FloderName)
        {
            string SavePath = SaveList(ImgList, FileName, FloderName);
            new AlertSplitList().ShowDialog();
            return SavePath;
        }

        /// <summary>
        /// 所有文件默认放在temp文件夹下，需要给的是子文件夹的名字，例如“半次元”
        /// </summary>
        /// <param name="ImgList"></param>
        /// <param name="ChildFloderName"></param>
        public static string DownLoadFileList(List<string> ImgList, string ChildFloderName)
        {
            if (ChildFloderName == "")
                ChildFloderName = "未命名资源";
            int CompltedCount = 0;
            string FlodePath = StaticValueOld.DownloadPath + string.Format("{0}\\", ChildFloderName);
            foreach (string Line in ImgList)
            {
                new PrintLog().PercentLog(ImgList.Count, ++CompltedCount);
                if (Line == "")
                    continue;

                FloderHelper.FloderExits(FlodePath, true);
                if (Line.IndexOf("pximg.net") >= 0)
                    WebHelper.DownloadToFloder(Line, FlodePath, new PixivHelper().PixivHeader(Line));
                else
                {
                    WebHelper.DownloadToFloder(Line, FlodePath, WebHelper.PcWebHeader(Line));
                }
            }
            return FlodePath;
        }
    }
}